<template>
  <el-dialog class="pick-dialog" title="取消订单" :visible.sync="dialogVisible" :close-on-click-modal="false" width="600px">
    <div class="form-box">
      <el-form :model="ruleForm" ref="ruleForm" label-width="100px" :rules="rules" class="demo-ruleForm">
        <div v-if="orderInfo" style="line-height: 1.7em;margin-bottom: 10px;">
          <div>
            订单号： <b>{{ orderInfo.no }}</b>
          </div>
          <div>
           订单金额： <span style="font-size: 18px;color: red;">¥{{ orderInfo.actualAmount }}</span>
          </div>
          <div  v-if="orderInfo.type==2&&orderInfo.salePay==2">
           该订单是B2B订单，买家 <span style="color: red;">已付款</span> ，取消交易会将<span style="color: red;">订单金额</span>全额退回买家账户余额。
          </div>
        </div>
        <el-form-item label="取消原因:" prop="remark">
          <el-select v-model="ruleForm.remark" v-if="from=='kefu'">
            <el-option v-for="item in reasonListKefu" :key="item.value" :label="item.label" :value="item.label"></el-option>
          </el-select>
          <el-select v-model="ruleForm.remark" v-else>
            <el-option v-for="item in reasonListBuyer" :key="item.value" :label="item.label" :value="item.label"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品处理:" prop="changeGoodsSaleState" v-if="from=='kefu'&&orderType!=2">
          <el-select v-model="ruleForm.changeGoodsSaleState" >
            <el-option v-for="(label, key) in goodsType" :key="key" :label="label" :value="key"></el-option>
          </el-select>
       
        </el-form-item>
        <div style="margin-bottom: 20px;" v-if="cancelType=='cancelAndRefund'">
          你正在进行取消订单并退款操作，确认后会将买家支付的 <span style="color: red;">货款原路退回</span> ，请谨慎操作!
        </div>
        <div>
          
        </div>
      </el-form>

      <div class="center">
        <el-button @click="close('ruleForm')">关闭</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')" :loading="loading">确认</el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import orderApi from '@/api/pickUp/order'
import {violationRulesList} from "@/api/weigui/index"
export default {
  name: "pick",
  data() {
    return {
      dialogVisible: false,
      loading: false,
      violationRulesIds:[],
      goodsType:{
        1:'上架',
        4:'下架',
        5:'转问题号',
      },
      reasonListKefu: [
        {
          label: '已沟通买家不想买了',
          value: '1'
        },
        {
          label: '已沟通买家迟迟不付款',
          value: '2'
        },
        {
          label: '恶意占号',
          value: '3'
        },
        {
          label: '其他原因',
          value: '4'
        },
      ],
      reasonListBuyer: [
        {
          label: '不想买了',
          value: '1'
        },
        {
          label: '选错号了',
          value: '2'
        },
        {
          label: '账号有问题',
          value: '3'
        },
        {
          label: '其他原因',
          value: '4'
        },
      ],
      ruleForm: { 
        remark:'',
        id:'',
        changeGoodsSaleState:''
      },
      rules:
      {
        changeGoodsSaleState:[{ required: true, message: '请选择商品处理结果', trigger: 'change' }],
        remark:[{ required: true, message: '请选择取消原因', trigger: 'change' }]
      },
      from:'',//来源
      weiguiList:[],
      orderType:2,
      orderInfo:null,
      cancelType:''
    };
  },
  created() { },
  methods: {
    open(row,from,type) {

      this.cancelType = type
      this.ruleForm.remark = ''
      this.orderInfo = {...row}
      this.from = from||''
      this.loading = false
      this.ruleForm.id = row.id;
      this.orderType = row.orderType;
      this.ruleForm.changeGoodsSaleState = '';
      this.ruleForm.violationRulesIds = '';
      this.dialogVisible = true;
      this.violationRulesIds = []
      this.$store.dispatch("getShopInfo")
    },
    submitForm(formName) {
      if(this.orderInfo.type==2&&this.orderInfo.orderStatus==4&&this.cancelType!='cancelAndRefund'){
        // B2B订单
        const shopInfo = this.$store.getters.shopInfo
        if(shopInfo.availableMoney<this.orderInfo.actualAmount){
          // 余额不足
         return this.$message.error('余额不足，无法完成退款！')
        }
      }
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.loading = true
          
          if(this.from=='kefu'){
            this.ruleForm.remark = '客服取消-' + this.ruleForm.remark
          }else{
            this.ruleForm.remark = '用户取消'+ this.ruleForm.remark
          }
          if(this.violationRulesIds&&this.violationRulesIds.length){
            this.ruleForm.violationRulesIds = this.violationRulesIds.join(',')
          }else{
            this.ruleForm.violationRulesIds = ''
          }
          if(this.orderInfo.type==2){
            this.ruleForm.type=this.orderInfo.type
          }
          if(this.cancelType=='cancelAndRefund'){
            // 取消订单并退款
            this.ruleForm.huifuRefundType = 2
            this.ruleForm.remark = this.ruleForm.remark+'-取消并退款'
          }
          orderApi.cancelOrder(this.ruleForm).then(res=>{
            //console.log("%c Line:93 🍬取消订单 res", "color:#42b983", res);
            this.dialogVisible = false;
            this.loading = false
            if(res.code==200){
              this.$message.success(res.msg)
              this.$parent.getList()
              this.$store.dispatch("getShopInfo")
            }

          }).catch(err=>{
            this.loading = false
          })
        } else {
          //console.log("error submit!!");
          return false;
        }
      });
    },
    // 查询违规类型 violationRulesList
    getViolationRulesList(){
      violationRulesList({
        isPage:2
      }).then(res=>{
        //console.log("%c Line:133 🍯 res", "color:#ea7e5c", res);
        this.weiguiList = res.rows
      })
    },
    close(formName) {
      this.dialogVisible = false
    }
  }
};
</script>
<style scoped lang="scss">

.demo-ruleForm {
  padding-right: 20px;
}

.center {
  text-align: center;
}



.pick-dialog>>>.el-input--medium input {
  text-align: left;
}</style>
